﻿$(function () {
    $("#content-grid").datagrid({
        url: $.Api("Gen/Content/Grid"),
        nowrap: false, rownumbers: true, border: false,
        fit: true, singleSelect: true,
        pageSize: 20, pagination: true,
        idField: "ID", toolbar: "#content-tools",
        columns:
        [[
            { field: "Name", title: "名称", width: 200, align: "center" },
            { field: "Tag", title: "标记", width: 150, align: "center" },
            { field: "Remark", title: "描述", width: 280, align: "center" },
            { field: "InsertTime", title: "添加时间", width: 140, align: "center", sortable: true },
            { field: "UpdateTime", title: "更新时间", width: 140, align: "center", sortable: true },
            { field: "IntSort", title: "排序", width: 50, align: "center" },
            {
                field: "IsVisible", title: "启用", width: 50, align: "center",
                formatter: function (v, r, i) { return $.GetYesOrNo(v); }
            }
        ]]
    });

    $("#content-searchbox").searchbox({
        width: 150, prompt: "请输入信息标题查询",
        searcher: function (value) {
            $("#content-grid").datagrid("clearChecked");
            $("#content-grid").datagrid("load", { search: value });
        }
    });
});

var Content = {
    //添加
    Insert: function () {
        Content.Mutual("添加", "gen/content/Edit.aspx");
    },
    //修改
    Update: function () {
        var row = $("#content-grid").datagrid("getSelected");
        if (row) {
            Content.Mutual("修改", "gen/content/Edit.aspx?id=" + row.ID);
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },
    //删除
    Delete: function () {
        var row = $("#content-grid").datagrid("getSelected");
        if (row) {
            $.EasyuiConfirm("确认删除?", function (del) {
                if (del) {
                    $.AjaxMask({
                        url: $.Api("Gen/Content/Delete"),
                        type: "post", data: { id: row.ID },
                        success: function (result) {
                            result = JSON.parse(result);
                            $.EasyuiSlide(result.Message);
                            if (result.Success) { Content.Reload(); }
                        }
                    });
                }
            });
        }
        else { $.EasyuiSlide(delnullrowmsg); }
    },
    //实体操作
    Mutual: function (title, href) {
        $.ModelDialog({
            title: title, href: href, width: 668,
            handler: function () {
                $.ModelForm({
                    validate: true, button: $(this),
                    editor: "#content-content",
                    id: "#content-model",
                    url: $.Api("Gen/Content/Save"),
                    success: function (result) { Content.Reload(); }
                });
            },
            onLoad: function () { Content.FormInit(); }
        });
    },
    //弹窗初始化
    FormInit: function () {
        $.EditorInit("#content-content", 200, "p", true);
    },

    //预览
    Preview: function () {
        var row = $("#content-grid").datagrid("getSelected");
        if (row) {
            window.open("../Content.aspx?tag=" + row.Tag);
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },

    //排序向上
    Up: function () {
        Content.Order("up");
    },
    //排序向下
    Down: function () {
        Content.Order("down");
    },
    //排序操作
    Order: function (order) {
        var row = $("#content-grid").datagrid("getSelected");
        if (row) {
            $.AjaxMask({
                url: $.Api("Gen/Content/SetOrder"),
                type: "post", data: { id: row.ID, order: order },
                success: function (result) {
                    result = JSON.parse(result);
                    $.EasyuiSlide(result.Message);
                    if (result.Success) { Content.Reload(); }
                }
            });
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },

    //启用
    Enable: function () {
        Content.EnableOrDisable(true);
    },
    //禁用
    Disable: function () {
        Content.EnableOrDisable(false);
    },
    //启用禁用
    EnableOrDisable: function (bool) {
        var row = $("#content-grid").datagrid("getSelected");
        if (row) {
            $.AjaxMask({
                url: $.Api("Gen/Content/EnableOrDisable"),
                type: "post", data: { id: row.ID, bit: bool },
                success: function (result) {
                    result = JSON.parse(result);
                    $.EasyuiSlide(result.Message);
                    if (result.Success) { Content.Reload(); }
                }
            });
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },

    //重置标记
    Reset: function () {
        var row = $("#content-grid").datagrid("getSelected");
        if (row) {
            $.ModelDialog({
                title: "重置标记", width: 260,
                href: "gen/content/Reset.aspx?id=" + row.ID,
                handler: function () {
                    $.ModelForm({
                        validate: true, button: $(this),
                        id: "#content-reset",
                        url: $.Api("Gen/Content/Reset"),
                        success: function (result) { Content.Reload(); }
                    });
                },
                onLoad: function () { }
            });
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },

    //刷新列表
    Reload: function () {
        $.DataGridReload("#content-grid");
    }
};